var ui = (function () {

  function closeConfirm() {
    var confirmElement = document.getElementsByClassName('ui-confirm')[0];
    document.body.removeChild(confirmElement);
  }

  /**
   * 确认框
   * @param {String} message 提示信息
   * @param {String} type 弹框类型 `complete fail`
   */
  function confirm(message, type) {
    type = type || 'fail';
    return new Promise(function (resolve, reject) {
      get('./ui/template/confirm.html').then(t => {
        document.body.insertAdjacentHTML('afterbegin', t);

        var confirmBody = document.getElementsByClassName('ui-confirm__body')[0];
        var img = new Image();
        img.classList.add('ui-confirm__icon');
        img.setAttribute('src', './ui/img/' + type + '.png');
        confirmBody.appendChild(img);
        confirmBody.appendChild(document.createTextNode(message));

        // add event
        document.getElementById('ui-confirm-ok').addEventListener('click', function () {
          closeConfirm();
          resolve('ok');
        });

        document.getElementById('ui-confirm-cancel').addEventListener('click', function () {
          closeConfirm();
          reject('cancel');
        });

      });
    })

  }

  /**
   *顶部提示
   * @param {String} message 提示信息
   * @param {String} type 类型 `success error warn`
   * @param {Number} delay 多少毫秒后关闭
   */
  function topTips(message, type, delay) {
    type = type || 'error';
    delay = delay || 2000;
    var tipsElement = document.createElement('div');
    var body = document.body;
    tipsElement.classList.add('ui-top-tips');
    tipsElement.classList.add(type);
    tipsElement.textContent = message;

    body.appendChild(tipsElement);
    setTimeout(function () {
      body.removeChild(tipsElement);
    }, delay);
  }


  var loading_opened = false;
  /**
   * 加载框
   * @param {Number} type 类型
   * @param {String} tips 提示信息
   */
  function loading(type, tips) {
    if (!loading_opened) {
      type = type || 1;
      tips = tips || 'Loading...';

      var mask = document.createElement('div');
      var con = document.createElement('div');
      var text = document.createElement('div');

      mask.className = 'ui-mask';
      con.className = 'ui-loading-con';
      text.className = 'ui-loading-text  ui-mc'
      text.innerHTML = tips;

      if (type == 1) {
        for (var i = 0; i < 5; i++) {
          var dot = document.createElement('div');
          dot.classList.add('ui-loading-dot');
          con.classList.add('loading-type-1');
          con.appendChild(dot);
        }
      }
      else if (type == 2) {
        var ani = document.createElement('div');
        ani.classList.add('dot-windmill');
        con.appendChild(ani);
      }

      document.body.appendChild(mask);
      mask.appendChild(con);
      mask.appendChild(text);

      loading_opened = true;
    }

    return {
      hide: function () {
        loading_opened = false;
        let m = document.getElementsByClassName('ui-mask')[0];
        if (m) {
          document.body.removeChild(m);
        }
      }
    }
  }

  /**
   * 在超过delay时间后显示loading
   * @param {*} tips 提示
   * @param {*} delay 延时
   */
  function delayLoading(tips, delay) {
    var _loading;
    if (delay == null) {
      delay = 500
    }
    var timerId = setTimeout(function () {
      _loading = ui.loading(tips)
    }, delay);

    return function () {
      clearTimeout(timerId);
      _loading && _loading.hide();
    };
  }


  return {
    confirm: confirm,
    topTips: topTips,
    loading: loading,
    delayLoading: delayLoading
  };

})();
